package model.backup.handlers;

import database.Database;
import java.sql.SQLException;
import model.backup.BackupOrder;

/**
 * Denne klasse bruges til at hente al data i bs_order og returnere det
 * @author junjie
 */
public abstract class BackupOrderHandler {
    
    /**
     * Henter alle rækker i bs_order ag returnerer et BackupOrder[] model
     * @return BackupOrder[]
     * @throws ClassNotFoundException
     * @throws SQLException 
     */
    public static BackupOrder[] getBackupOrders() throws ClassNotFoundException, SQLException{
        BackupOrder[] backupOrders = null;
        
        Database db = new Database();
        
        String[][] result = db.getFromDB("bs_order", "", "", "", "id", "order_date", "pickup_date", "delivery_date", "invoice_date", "payment_received", "finished", 
                                        "comment", "fk_user_id", "fk_tax", "fk_tombstone_order_id");
        backupOrders = new BackupOrder[result.length];
        
        db.closeDB();
        
        for (int i = 0; i < result.length; i++) 
        {            
            String orderDate = "";
            if (result[i][1] == null)
            {
                orderDate = "0000-00-00 00-00";
            }
            else
            {
                orderDate = result[i][1];
            }
            
            String pickupDate = "";
            if (result[i][2] == null)
            {
                pickupDate = "0000-00-00 00-00";
            }
            else
            {
                pickupDate = result[i][1];
            }
            
            String deliveryDate = "";
            if (result[i][3] == null)
            {
                deliveryDate = "0000-00-00 00-00";
            }
            else
            {
                deliveryDate = result[i][1];
            }
            
            String invoiceDate = "";
            if (result[i][4] == null)
            {
                invoiceDate = "0000-00-00 00-00";
            }
            else
            {
                invoiceDate = result[i][1];
            }
            
            int tombStoneID = 0;
            if (result[i][10] != null)
            {
                tombStoneID = Integer.parseInt(result[i][10]);
            }
            
            backupOrders[i] = new BackupOrder(Integer.parseInt(result[i][0]), orderDate, pickupDate, deliveryDate, invoiceDate,
                                              Double.parseDouble(result[i][5]), Integer.parseInt(result[i][6]), result[i][7] , Integer.parseInt(result[i][8]),
                                              Integer.parseInt(result[i][9]), tombStoneID);
        }
        return backupOrders;
    }
}
